import {Component, Input, OnInit, ViewChild,ChangeDetectionStrategy} from '@angular/core';
import {ColumnType, SPage, TablePageComponent} from "@shared/components/table-page/table-page.component";
import {NzMessageService, NzModalService,NzModalRef} from "ng-zorro-antd";
import { UploadFile } from 'ng-zorro-antd/upload';
import {AreaService} from "../../sys-setting/area-manage/area.service";
import {ApiService} from "@core/services/api";
import {HttpClient} from "@angular/common/http";
import { ChangeDetectorRef } from '@angular/core';
import { XlsxService } from '@delon/abc/xlsx';
import { NzListModule } from 'ng-zorro-antd/list';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-add-building-batch',
  templateUrl: './add-building-batch.component.html',
  styles: []
})
export class AddBuildingBatchComponent implements OnInit {
  @Input() id;
  validateForm!: FormGroup;
  batchAdd = {
    startHouse: null,
    terminationHouse: null,
    startHost: null,
    terminationHost: null
  }

  constructor(
    private modal: NzModalService,
    private msg: NzMessageService,
    private area: AreaService,
    private api: ApiService,
    private http: HttpClient,
    private modalRef: NzModalRef,
    private xlsx: XlsxService,
    private cdr: ChangeDetectorRef,
    private fb: FormBuilder
  ) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      startHouse:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
      terminationHouse:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
      startHost:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
      terminationHost:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
    });
  }
  dataProcess(){
    
  }
  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsPristine();
      this.validateForm.controls[key].updateValueAndValidity();
    }
  }
  //批量
  submitForm(data): void {
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
      if(this.validateForm.controls[key].invalid){
        return;
      }
    }
    const body = {
      startHouse: data.startHouse,
      terminationHouse: data.terminationHouse,
      startHost: data.startHost,
      terminationHost: data.terminationHost,
      areaId: this.id
    }
    this.http.request<any>("post","/core/house/saveBatch", {body:body}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.msg.success(res.message);
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })
  }
}
